package com.DeliveryProgram.www.dao;

import com.DeliveryProgram.www.po.Business;
import com.DeliveryProgram.www.po.Menu;
import com.DeliveryProgram.www.util.JDBCutil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class BusSave {
    /**将商家修改的数据存入数据库中*/

    private static Connection conn=null;
    private static Statement stmt=null;
    private static ResultSet rs=null;

    /**处理用户对店名的修改*/
    public static void saveName(Business bu){
        try {
            conn= JDBCutil.getConnection();
            String sql="UPDATE business SET shopname='"+bu.getShopname()+"' WHERE bid="+bu.getBid();
            stmt=conn.createStatement();
            stmt.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(stmt,conn);
        }
    }

    /**处理用户对简介的修改*/
    public static void saveIntroduce(Business bu){
        try {
            conn= JDBCutil.getConnection();
            String sql="UPDATE business SET introduce='"+bu.getIndroduce()+"' WHERE bid="+bu.getBid();
            stmt=conn.createStatement();
            stmt.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(stmt,conn);
        }
    }

    /**修改订单状态*/
    public static void changeState(Business bu,int num,int newState){
        try {
            conn=JDBCutil.getConnection();
            String sql="UPDATE orders SET isComplete="+newState+" WHERE id="+bu.getOrders().get(num).getId();
            stmt=conn.createStatement();
            stmt.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            JDBCutil.close(stmt,conn);
        }
    }

    /**添加新的评论*/
    public static void addComment(String comment){
        try {
            conn=JDBCutil.getConnection();
            String sql="INSERT INTO COMMENT(COMMENT) VALUES('"+comment+"')";
            stmt=conn.createStatement();
            stmt.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            JDBCutil.close(stmt,conn);
        }
    }

    /**将关系存入商户评论表中*/
    public static void addRelationship(Business bu){
        try {
            conn=JDBCutil.getConnection();
            String sql="INSERT INTO businesscomment(id,business) VALUES("+bu.getComments().get(bu.getComments().size()-1).getId()+","+bu.getBid()+")";
            stmt=conn.createStatement();
            stmt.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(stmt,conn);
        }
    }

    /**添加新的菜单*/
    public static void addNewMenu(Business bu,String name,String introduce,double price){
        try {
            conn=JDBCutil.getConnection();
            String sql="INSERT INTO menu(NAME,introduce,price,business) VALUES('"+name+"','"+introduce+"',"+price+","+bu.getBid()+")";
            stmt=conn.createStatement();
            stmt.execute(sql);
            bu.setMenu(new Menu(name,introduce,price));
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(stmt,conn);
        }
    }

    /**删除某菜单*/
    public static void deleteMenu(Menu mu){
        try {
            conn=JDBCutil.getConnection();
            String sql="DELETE FROM menu WHERE id="+mu.getId();
            stmt=conn.createStatement();
            stmt.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(stmt,conn);
        }
    }

    /**更改菜名*/
    public static void changMenuName(Business bu,int num){
        try {
            conn=JDBCutil.getConnection();
            String sql="UPDATE menu SET NAME='"+bu.getMenu().get(num).getName()+"' WHERE id="+bu.getMenu().get(num).getId();
            stmt=conn.createStatement();
            stmt.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(stmt,conn);
        }
    }

    /**更改菜品介绍*/
    public static void changMenuIntroduce(Business bu,int num){
        try {
            conn=JDBCutil.getConnection();
            String sql="UPDATE menu SET introduce ='"+bu.getMenu().get(num).getIntroduce()+"' WHERE id="+bu.getMenu().get(num).getId();
            stmt=conn.createStatement();
            stmt.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(stmt,conn);
        }
    }

    /**更改菜品价格*/
    public static void changMenuPrice(Business bu,int num){
        try {
            conn=JDBCutil.getConnection();
            String sql="UPDATE menu SET price ='"+bu.getMenu().get(num).getPrice()+"' WHERE id="+bu.getMenu().get(num).getId();
            stmt=conn.createStatement();
            stmt.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(stmt,conn);
        }
    }

    /**添加新的折扣*/
    public static void addCoupons(Business bu){
        try {
            conn=JDBCutil.getConnection();
            String sql="INSERT INTO coupons(business,mount,discount) VALUES("+bu.getBid()+","+bu.getCoupons().get(bu.getCoupons().size()-1).getMount()+","+bu.getCoupons().get(bu.getCoupons().size()-1).getDiscount()+")";
            stmt=conn.createStatement();
            stmt.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(stmt,conn);
        }
    }

    /**改变折扣数量*/
    public static void changeCouponMount(Business bu,int num){
        try {
            conn=JDBCutil.getConnection();
            String sql="UPDATE coupons SET mount='"+bu.getCoupons().get(num).getMount()+"' WHERE id="+bu.getCoupons().get(num).getId();
            stmt=conn.createStatement();
            stmt.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(stmt,conn);
        }
    }

    /**改变折扣力度*/
    public static void changeCouponDiscount(Business bu,int num){
        try {
            conn=JDBCutil.getConnection();
            String sql="UPDATE coupons SET discount='"+bu.getCoupons().get(num).getDiscount()+"' WHERE id="+bu.getCoupons().get(num).getId();
            stmt=conn.createStatement();
            stmt.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(stmt,conn);
        }
    }

}
